package com.hghivln.erpwarehouse.system.controller;


import com.hghivln.erpwarehouse.system.common.lang.ActiverUser;
import com.hghivln.erpwarehouse.system.common.lang.DataGridlView;
import com.hghivln.erpwarehouse.system.common.lang.Result;
import com.hghivln.erpwarehouse.system.service.NoticeService;
import com.hghivln.erpwarehouse.system.vo.NoticeVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hghivln
 * @since 2021-06-26
 */
@RestController
@RequestMapping("api/notice")
public class NoticeController {

    @Autowired
    private NoticeService noticeService;

    @RequestMapping("loadAllNotice")
    public DataGridlView loadAllNotice(NoticeVo noticeVo) {
        return noticeService.queryAllNotice(noticeVo);
    }

    @RequestMapping("addNotice")
    @RequiresPermissions("notice:add")
    public Result addNotice(NoticeVo noticeVo) {
        try {
            ActiverUser activerUser = (ActiverUser) SecurityUtils.getSubject().getPrincipal();
            noticeVo.setCreatetime(new Date());
            noticeVo.setOpername(activerUser.getUser().getLoginname()+"【"+activerUser.getUser().getName()+"】");
            noticeService.save(noticeVo);
            return Result.ADD_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return Result.ADD_ERROR;
        }
    }

    @RequestMapping("updateNotice")
    @RequiresPermissions("notice:update")
    public Result updateNotice(NoticeVo noticeVo) {
        try {
            noticeService.updateById(noticeVo);
            return Result.UPDATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return Result.UPDATE_ERROR;
        }
    }

    @RequestMapping("deleteNotice")
    @RequiresPermissions("notice:delete")
    public Result deleteNotice(Integer id) {
        try {
            noticeService.removeById(id);
            return Result.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return Result.DELETE_ERROR;
        }
    }

    @RequestMapping("batchDeleteNotice")
    @RequiresPermissions("notice:batchdelete")
    public Result batchDeleteNotice(Integer[] ids) {
        try {
            Collection<Serializable> idList = new ArrayList<>(Arrays.asList(ids));
            noticeService.removeByIds(idList);
            return Result.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return Result.DELETE_ERROR;
        }
    }
}
